package br.edu.ufcg.dsc.actions.search;

import org.apache.struts2.config.ParentPackage;
import org.apache.struts2.config.Result;
import org.apache.struts2.config.Results;

import br.edu.ufcg.dsc.domain.Resource;
import br.edu.ufcg.dsc.domain.ResourceType;
import br.edu.ufcg.dsc.services.ResourceService;

import com.opensymphony.xwork2.ActionChainResult;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@ParentPackage(value = "myPackage")
@Results(value = {
		@Result(name = "book", type = ActionChainResult.class, value = "findBook", params = {"namespace", "/resource"}),
		@Result(name = "article", type = ActionChainResult.class, value = "findArticle", params = {"namespace", "/resource"}),
		@Result(name = "issue",type = ActionChainResult.class, value = "findIssue", params = {"namespace", "/resource"})	
})
public class DetailsResourceAction extends ActionSupport{

	private static final long serialVersionUID = 1L;
	private Resource resource;
	private int id;
	
	@SuppressWarnings("unchecked")
	@Override
	public String execute(){
		ActionContext.getContext().getParameters().put("resourceId", id);
		resource = ResourceService.find(id);
		if (resource.getType() == ResourceType.BOOK)
			return "book";
		else if (resource.getType() == ResourceType.ARTICLE)
			return "article";
		else if (resource.getType() == ResourceType.ISSUE)
			return "issue";
		return SUCCESS;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}
	
	

}
